11. Lesson Review
In this lesson we learned…
How can I get physics setup in my project?
Adding physics to our VR projects help our virtual environments feel more realistic. To use Unity’s physics system, we’ll need to add a collider on our 3D objects. Unity has box, capsule, sphere, and mesh colliders. Colliders help define areas where we want physics to work on.
Tip: Primitive colliders are more performant than mesh colliders. Use them when you can to improve performance.
We’ll also need to add a Rigidbody component to our object. This is what will actually be used to calculate forces like gravity, drag and mass.
What is raycasting?
Raycasting is a way we can simulate looking for something.
For example, let’s say we lost our keys or someone moved them. Our eyes would scan around the room looking for them. When we find them, we can pick them up and go about our day.
In that example, imagine we’re drawing a line from our eyes to the point we are looking at. That line would move around the room as we look. That’s what raycasting does. It allows us to start at one point (our eyes) and draw a line (look) in a direction. If our line intersects with something we care about (our keys), we can take action. If not, we continue our search.
To use Raycasting in Unity, we need two pieces of information. The point we start at (like our eyes) and the directions we’re looking (forward). Because raycasting uses the Unity physics engine, FixedUpdate is the best place for this code. FixedUpdate works like the Update method, but instead of updating every frame, it runs it at fixed intervals (whenever there’s a fixed framerate frame).
Our call to Physics.Raycast takes a starting point, our direction, and the distance you want the ray to go.
void FixedUpdate()
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, 10))
print("There is something in front of the object!");
}
Tip: Rigidbodies and Physics code should go in the FixedUpdate method instead of the Update method.
Setup Objects For Use With Raycasting
Anything we want to detect with a raycast needs to have a collider on it.
Learn more about raycasting by checking out the Unity Documentation for Raycasting.
How can I add audio in my scene?
First, we need to be sure we’ve include the audio file into our project. Bringing an audio file into Unity, it is automatically converted into an audio clip.
On the object that we want to play the sound, we’ll add an Audio Source component. Then click and drag the audio clip into the “AudioCilp” slot of the Audio Source component.
To ensure our audio is setup properly for VR, we’ll change the Spacial Blend property. In the real world, the closer we are to an object making noise, the louder it is. Most of the audio we use in VR we’ll want to be setup similarly. Move the Spacial blend slider to 3D to make our audio realistic. If you’re wanting to add things like background audio clips or anything that you want the users to be able to hear the same way regardless of their position in the scene, we’ll move this slider to 2D.
In order for audio to work properly in our scenes, we’ll need some audio sources (the objects that make the sounds) and one audio listener. The audio listener is typically found on the main camera (or on the camera rig in many VR sdks).
What are arrays? How do I create one?
Arrays are a common type of storing data. An easy way of thinking about an array is to think of a file cabinet. A filing cabinet has drawers and in each drawer we’re able to store things (our data).
We indicate an array by specifying the data type we wish to store followed by open and closing square brackets ([ ]). Public arrays (like in the example code below) will allow us to set the array size and fill the array in the Unity editor.
public GameObject[] objectArray;
public string[] myStringCollection;
To access the data from an array, we need to determine the index of the data we want to retrieve. Essentially, we want to know what slot to pull the data from. Arrays are “zero-indexed”, which means that the numbering starts at 0 instead of 1. The first slot in an array is 0, the second is 1, and so on. Once we have the index, we’ll use the square brackets to get the data out.
public string[] myStringCollection;
void PrintAString(int index) {
Debug.Log(“The string at index “ + index + “ is “ + myStringCollection[index]);
// This will print: “The string at index [index we pass into the method] is [string in the collection]”
}
// If we don’t want to set the data in the inspector, we can also do it through code.
void SetupStringCollection() {
// Set the collection to a new array with the size of 5.
myStringCollection = new string[5];
// Adds our data to the array.
for(var i = 0; i < 5; i++) {
// Sets the string at the current value of i to be whatever i * 100 is. For example, if i is 2, the first slot in myStringCollection would be the string representation of 200.
myStringCollection[ i ] = ( i * 100 ).ToString();
}
}
How can I get a random number?
To generate a random number in a script, you can use the Random.Range method. This method takes in two values. The first is the minimum, the second is the maximum. This method uses the maximum as the first number that would NOT be returned. For example, if we do something like the line below, we could get a value as low as -10 and as high as 9:
Random.Range(-10f, 10f);